package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/6/18 09:47
 * @apiNote
 */

public class Test02288DiscountPrices {

    public static void main(String[] args) {
        String sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$";
        int discount = 100;
        String result = discountPrices(sentence, discount);
        System.out.println(result);
    }

    public static String discountPrices(String sentence, int discount) {
        StringBuilder result = new StringBuilder();
        String[] split = sentence.split(" ");
        double div = discount/100.00;
        for(String str: split){
            if(str.charAt(0) == '$' && isNumeric(str.substring(1))){
                String temp = str.substring(1);
                double price = Double.parseDouble(temp) - Double.parseDouble(temp) * div;
                result.append("$").append(String.format("%.2f", price)).append(" ");
            }else{
                result.append(str).append(" ");
            }
        }
        return result.substring(0, result.length()-1);
    }

    public static boolean isNumeric(String s) {
        if (s.isEmpty()) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isDigit(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

}
